/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */

package cn.com.ksplatform.modules.system.auth.permitmanager.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.ksplatform.base.BaseController;
import cn.com.ksplatform.base.Return;
import cn.com.ksplatform.base.SimpleReturn;
import cn.com.ksplatform.base.service.BaseService;
import cn.com.ksplatform.modules.system.auth.buttonmanager.model.PlatButton;
import cn.com.ksplatform.modules.system.auth.buttonmanager.model.PlatButtonGroup;
import cn.com.ksplatform.modules.system.auth.buttonmanager.service.PlatButtonService;
import cn.com.ksplatform.modules.system.auth.cachemanager.service.SystemCacheService;
import cn.com.ksplatform.modules.system.auth.menumanager.model.PlatMenu;
import cn.com.ksplatform.modules.system.auth.menumanager.service.PlatMenuService;
import cn.com.ksplatform.modules.system.auth.permitmanager.service.PermitService;
import cn.com.ksplatform.modules.system.auth.resourcemanager.model.PlatResource;
import cn.com.ksplatform.modules.system.auth.resourcemanager.service.PlatResourceService;
import cn.com.ksplatform.modules.system.auth.rolemanager.model.PlatRole;
import cn.com.ksplatform.modules.system.auth.usermanager.model.PlatUser;
import cn.com.ksplatform.modules.system.auth.usermanager.service.PlatUserService;

/**
 * @ClassName:PermitController <br/>
 * @deprecated:TODO. <br/>
 * @Date: 2015年6月23日 上午10:38:50 <br/>
 * 
 * @author Mr.Luoxing
 * @version 1.0
 */
@Controller
@RequestMapping("/modules/system/permit")
public class PermitController extends BaseController {

	PermitService permitService;
	
	@Resource(name = "system.modules.system.PlatMenuService")
	PlatMenuService menuService;
	
	@Resource(name = "system.modules.system.PlatButtonService")
	PlatButtonService buttonService;
	
	@Resource(name = "system.modules.system.PlatResourceService")
	PlatResourceService resourceService;
	
	@Resource(name = "system.modules.system.PlatUserService")
	PlatUserService userService;
	
	@Autowired
	SystemCacheService systemCacheService;
	
	@Resource(name = "system.modules.system.PermitService")
	@Override
	public void setBaseService(BaseService baseService) {
		permitService = (PermitService) baseService;
		super.setBaseService(baseService);
	}

	@SuppressWarnings("rawtypes")
	@RequestMapping("role")
	@ResponseBody
	public List grid() throws Exception {
		return (List) super.findAll(PlatRole.class).ReturnObjs;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping("tree")
	@ResponseBody
	public List<PlatMenu> tree() throws Exception {
		List<PlatMenu> menus = (List<PlatMenu>) super.findAll(PlatMenu.class).ReturnObjs;
		return menus;
	}

	@RequestMapping("resource")
	@ResponseBody
	public Return resource() throws Exception {
		return super.findAll(PlatResource.class);
	}

	@RequestMapping("buttons")
	@ResponseBody
	public Return buttons() throws Exception {
		return super.findAll(PlatButtonGroup.class);
	}

	@SuppressWarnings("rawtypes")
	@RequestMapping("stations")
	@ResponseBody
	public List findStations(@RequestParam String staId) {
		return permitService.findStations(staId);
	}

	@SuppressWarnings("rawtypes")
	@RequestMapping("findUsers")
	@ResponseBody
	public List findUsers(@RequestParam String roleId) {
		return permitService.findUsers(roleId);
	}

	// 添加角色和菜单的关系
	@RequestMapping("updateQuoteOfMenu")
	@ResponseBody
	public Return updateQuoteOfMenu(@RequestParam String menuId, @RequestParam String roleId, @RequestParam boolean update) {
		PlatMenu menu = (PlatMenu) super.load(PlatMenu.class, menuId).ReturnObjs;
		PlatRole role = (PlatRole) super.load(PlatRole.class, roleId).ReturnObjs;
		if (update) {
			menu.getRoles().add(role);
			systemCacheService.addMenuToCache(roleId, menu);
		} else {
			menu.getRoles().remove(role);
			systemCacheService.removeMenuToCache(roleId,menu);
		}
		return SimpleReturn.success("操作成功",menuService.update(menu));
	}

	// 添加角色和按钮的关系
	@RequestMapping("updateQuoteOfBtn")
	@ResponseBody
	public Return updateQuoteOfBtn(@RequestParam String resId, @RequestParam String roleId, @RequestParam boolean update) {
		PlatButton button = (PlatButton) super.load(PlatButton.class, resId).ReturnObjs;
		PlatRole role = (PlatRole) super.load(PlatRole.class, roleId).ReturnObjs;
		if (update) {
			button.getRoles().add(role);
			role.getBtns().add(button);
			systemCacheService.addButtonToCache(roleId,button);
		} else {
			button.getRoles().remove(role);
			role.getBtns().remove(button);
			systemCacheService.removeButtonToCache(roleId,button);
		}
		return SimpleReturn.success("操作成功",buttonService.update(button));
	}

	// 添加角色和资源的关系
	@RequestMapping("updateQuoteOfRes")
	@ResponseBody
	public Return updateQuoteOfRes(@RequestParam String resId, @RequestParam String roleId, @RequestParam boolean update) {
		PlatResource resource = (PlatResource) super.load(PlatResource.class, resId).ReturnObjs;
		PlatRole role = (PlatRole) super.load(PlatRole.class, roleId).ReturnObjs;
		if (update) {
			resource.getRoles().add(role);
			role.getResources().add(resource);
			systemCacheService.addResToCache(roleId, resource);
		} else {
			resource.getRoles().remove(role);
			role.getResources().remove(resource);
			systemCacheService.removeResToCache(roleId, resource);
		}
		return SimpleReturn.success("操作成功",resourceService.update(resource));
	}

	@RequestMapping("updateQuoteOfUser")
	@ResponseBody
	public Return updateQuoteOfUser(@RequestParam String userId, @RequestParam String roleId, @RequestParam boolean update) {
		PlatUser user = (PlatUser) super.load(PlatUser.class, userId).ReturnObjs;
		PlatRole role = (PlatRole) super.load(PlatRole.class, roleId).ReturnObjs;
		if (update) {
			user.getRoles().add(role);
		} else {
			user.getRoles().remove(role);
		}
		return SimpleReturn.success("操作成功",userService.update(user));
	}
	
	@RequestMapping("findMenus")
	@ResponseBody
	public List findMenus(String roleId){
		return permitService.findMenus(roleId);
	}
	
	@RequestMapping("findBtns")
	@ResponseBody
	public List findBtns(String roleId,String btnGId){
		return permitService.findBtns(roleId,btnGId);
	}
	
	@RequestMapping("findRes")
	@ResponseBody
	public List findRess(String roleId,String menuId){
		return permitService.findRes(roleId,menuId);
	}
	
}
